// pages/blog/blog.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    wx.request({
      url: app.globalData.host + 'Essay/article_pagination/' + that.data.page+'/10',
      success:function(res){
        if (res.statusCode == 200) {
          res.data=res.data.map((val)=>{
            val.create_time=val.create_time.slice(0, 11)
            return val
          })
          that.setData({
            list: res.data,
            blogList: res.data
          })
        } else {
          wx.showModal({
            title: '抱歉',
            content: '数据跑到外太空了,我们会抓紧把他找回来为小主服务😍'
          })
        }
      }
    })
    wx.setNavigationBarTitle({
      title: '我的博客'
    })
  },
  toDetail: function (e) {
    var index = e.currentTarget.dataset.index;
    let id = this.data.blogList[index].id
    wx.navigateTo({
      url: '/pages/content/content?id=' + id,
    })
  },
  bindKeyInput:function(e){
    this.setData({
      inputValue: e.detail.value.trim()
    })
    this.search();
  },
  search:function() {
    var that =this;
    if (that.data.inputValue){
      wx.request({
        url: app.globalData.host + 'Essay/search_essay/' + that.data.inputValue,
        success: function (res) {
          if (res.statusCode==200){
            
            if (res.data.length > 0) {
              res.data = res.data.map((val) => {
                val.create_time = val.create_time.slice(0, 11)
                return val
              })
              that.setData({
                blogList: res.data
              })
            } else {
              that.setData({
                blogList: that.data.list
              })
              wx.showModal({
                content: '不好意思😅，主人有点懒，还没有写出此文'
              })
            }
          } else {
            wx.showModal({
              title: '抱歉',
              title: '抱歉',
              content: '数据跑到外太空了,我们会抓紧把他找回来为小主服务😍'
            })
          }
        }
      })
    }else{
      that.setData({
        blogList: that.data.list
      })
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
 
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this;
    this.data.page++;
    wx.request({
      url: app.globalData.host + 'Essay/article_pagination/' + that.data.page + '/10',
      success: function (res) {
        if (res.statusCode == 200) {
          if (res.data.length) {
            res.data = res.data.map((val) => {
              val.create_time = val.create_time.slice(0, 11)
              return val
            })
            let list = that.data.list
            list = list.concat(res.data);
            that.setData({
              list: list,
              blogList: list
            })
          } else {
            wx.showModal({
              title: '您好',
              content: '我是有底线的哦🤔'
            })
          }
        }else{
          wx.showModal({
            title: '抱歉',
            content: '数据跑到外太空了,我们会抓紧把他找回来为小主服务😍'
          })
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})